"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toRef = exports.toRefs = exports.ref = exports.isRef = void 0;
var utils_1 = require("../utils");
var reactive_1 = require("./reactive");
var effect_1 = require("./effect");
exports.isRef = function (v) { return v ? v.__v_isRef === true : false; };
var convert = function (val) {
    return utils_1.isObject(val) ? reactive_1.reactive(val) : val;
};
function ref(value) { return createRef(value); }
exports.ref = ref;
function createRef(rawVal) {
    if (exports.isRef(rawVal)) {
        return rawVal;
    }
    var value = convert(rawVal);
    var depsMap = effect_1.createDepsMap();
    var r = {
        __v_isRef: true,
        get value() {
            effect_1.track(depsMap, 'value');
            return value;
        },
        set value(newVal) {
            if (rawVal !== newVal) {
                rawVal = newVal;
                value = convert(newVal);
                effect_1.trigger(depsMap, 'value');
            }
        }
    };
    return r;
}
function toRefs(object) {
    if (!reactive_1.isReactive(object)) {
        console.warn("toRefs() \u9700\u8981\u4E00\u4E2Areactive\u5BF9\u8C61\uFF0C\u4F46\u6536\u5230\u7684\u662F\u4E00\u4E2A\u666E\u901A\u5BF9\u8C61");
    }
    var ret = {};
    for (var key in object) {
        ret[key] = toRef(object, key);
    }
    return ret;
}
exports.toRefs = toRefs;
function toRef(object, key) {
    return {
        __v_isRef: true,
        get value() {
            return object[key];
        },
        set value(newVal) {
            object[key] = newVal;
        }
    };
}
exports.toRef = toRef;
